﻿using Crgd.UAuthServer.T4.Configs;
using Crgd.UAuthServer.T4.Entities;
using Crgd.UAuthServer.T4.Extensions;
using DreamX.T4.Entities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace DreamX.T4.Extensions
{
    public static class DbTableExtension
    {
        private static IDictionary<GenerateCodeType, string> _generateNameDictionary;
        private static IDictionary<GenerateCodeType, string> GenerateNameDictionary
        {
            get
            {
                if(_generateNameDictionary == null)
                {
                    lock (_generateNameDictionary)
                    {
                        if(_generateNameDictionary == null)
                        {
                            _generateNameDictionary = new Dictionary<GenerateCodeType, string>();
                            _generateNameDictionary.Add(GenerateCodeType.Entity, "Entities");
                            _generateNameDictionary.Add(GenerateCodeType.Configuration, "EntityConfiguration");
                            _generateNameDictionary.Add(GenerateCodeType.Dto, "Dtos");
                            _generateNameDictionary.Add(GenerateCodeType.EventHandler, "Events");
                            _generateNameDictionary.Add(GenerateCodeType.IService, "Services");
                            _generateNameDictionary.Add(GenerateCodeType.Service, "Sevices");
                            _generateNameDictionary.Add(GenerateCodeType.Controller, "Controllers");
                        }
                    }
                }

                return _generateNameDictionary;
            }
        }

        public static string GetNamespace(this DbTable dbTable, GenerateCodeType generateCodeType, T4Config config)
        {
            if(generateCodeType == GenerateCodeType.Configuration || generateCodeType == GenerateCodeType.Controller)
            {
                return $"{config.ProjectName}.{GenerateNameDictionary[generateCodeType]}";
            }
            return $"{config.ProjectName}.Core.{GenerateNameDictionary[generateCodeType]}";
        }

        public static string GetFileFullPath(this DbTable dbTable, GenerateCodeType generateCodeType, T4Config config)
        {
            var root = config.GenerateRootPath ?? string.Empty;
            var tempMoudle = string.IsNullOrEmpty(dbTable.MoudleName) ? string.Empty : dbTable.MoudleName;

            if(generateCodeType == GenerateCodeType.Configuration)
            {
                return Path.Combine(root, $"{config.ProjectName}.{GenerateNameDictionary[generateCodeType]}", tempMoudle).ToFullPath();
            }

            if(generateCodeType == GenerateCodeType.Controller)
            {
                return Path.Combine(root, $"{config.ProjectName}.WebApi", tempMoudle, GenerateNameDictionary[generateCodeType]).ToFullPath();
            }

            return Path.Combine(root, $"{config.ProjectName}.Core", GenerateNameDictionary[generateCodeType]).ToFullPath();
        }
    }
}
